package com.example.demo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohei.crm.common.exception.XiaoheiException;
import com.xiaohei.crm.common.result.Result;
import com.xiaohei.crm.dao.EnterpriseServices;
import com.xiaohei.crm.dao.Region;
import com.xiaohei.crm.mapper.EnterpriseServicesMapper;
import com.xiaohei.crm.mapper.OrdersMapper;
import com.xiaohei.crm.mapper.RegionMapper;
import com.xiaohei.crm.service.EnterpriseServicesService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.xiaohei.crm.common.RedisEnums.ES_REGISTRY_KEY;
import static com.xiaohei.crm.common.result.ResultCodeEnum.DATA_ERROR;


@Service
@RequiredArgsConstructor
public class EnterpriseServicesServiceImpl extends ServiceImpl<EnterpriseServicesMapper, EnterpriseServices> implements EnterpriseServicesService {

    private final OrdersMapper ordersMapper;
    private final EnterpriseServicesMapper enterpriseServicesMapper;
    private final StringRedisTemplate stringRedisTemplate;
    private final RegionMapper regionMapper;


    @Override
    public Result<IPage> findByPage() {
        Page<EnterpriseServices> page = new Page<>();
        LambdaQueryWrapper<EnterpriseServices> eq = Wrappers.lambdaQuery(EnterpriseServices.class).eq(EnterpriseServices::getDelFlag, 0);
        Page<EnterpriseServices> enterpriseServicesPage = enterpriseServicesMapper.selectPage(page, eq);
        for (EnterpriseServices record :enterpriseServicesPage.getRecords()) {
            Region region = regionMapper.selectById(record.getBusinessRegion());
            if(region!=null){
                record.setBusinessRegionName(region.getArea());
            }
        }
        return Result.ok(enterpriseServicesPage);
    }

    @Override
    public void saveMaterial(EnterpriseServices requestParams) {
        String s = stringRedisTemplate.opsForValue().get(ES_REGISTRY_KEY + requestParams.getId());
        if(!StrUtil.isEmpty(s)){
            throw new XiaoheiException(DATA_ERROR);
        }

        int isInsert = baseMapper.insert(requestParams);
        if(isInsert < 0){
            throw new XiaoheiException(DATA_ERROR);
        }

        stringRedisTemplate.opsForValue()
                .set(ES_REGISTRY_KEY + requestParams.getId(),requestParams.getId().toString());
    }

    @Override
    public void updateMaterialById(EnterpriseServices requestParams) {
        String s = stringRedisTemplate.opsForValue().get(ES_REGISTRY_KEY + requestParams.getId());
        if(StrUtil.isEmpty(s)){
            throw new XiaoheiException(DATA_ERROR);
        }

        LambdaQueryWrapper<EnterpriseServices> eq =
                Wrappers.lambdaQuery(EnterpriseServices.class)
                        .eq(EnterpriseServices::getId, requestParams.getId());
        baseMapper.update(requestParams, eq);
    }

    @Override
    public void removeByMaterialId(String id) {
        String s = stringRedisTemplate.opsForValue().get(ES_REGISTRY_KEY + id);
        if(StrUtil.isEmpty(s)){
            throw new XiaoheiException(DATA_ERROR);
        }

        LambdaUpdateWrapper<EnterpriseServices> updateWrapper =
                Wrappers.lambdaUpdate(EnterpriseServices.class).eq(EnterpriseServices::getId,id);
        EnterpriseServices enterpriseServices = EnterpriseServices.builder().delFlag(1).build();
        int update = baseMapper.update(enterpriseServices, updateWrapper);
        if(update < 1){
            throw new XiaoheiException(DATA_ERROR);
        }

        stringRedisTemplate.delete(ES_REGISTRY_KEY + id);
    }

}
